package cn.yh.nft.turbo.collection.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.yh.nft.turbo.api.chain.service.ChainFacadeService;
import cn.yh.nft.turbo.api.collection.model.CollectionVO;
import cn.yh.nft.turbo.api.collection.model.HeldCollectionVO;
import cn.yh.nft.turbo.api.collection.request.CollectionPageQueryRequest;
import cn.yh.nft.turbo.api.collection.request.HeldCollectionPageQueryRequest;
import cn.yh.nft.turbo.api.collection.service.CollectionFacadeService;
import cn.yh.nft.turbo.api.collection.service.CollectionReadFacadeService;
import cn.yh.nft.turbo.api.goods.service.GoodsFacadeService;
import cn.yh.nft.turbo.api.user.service.UserFacadeService;
import cn.yh.nft.turbo.base.response.PageResponse;
import cn.yh.nft.turbo.base.response.SingleResponse;
import cn.yh.nft.turbo.collection.domain.service.impl.HeldCollectionService;
import cn.yh.nft.turbo.web.util.MultiResultConvertor;
import cn.yh.nft.turbo.web.vo.MultiResult;
import cn.yh.nft.turbo.web.vo.Result;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: CollectionController
 * @Author: 草莓爱上啵啵
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("collection")
public class CollectionController {

    @Autowired
    private GoodsFacadeService goodsFacadeService;
    @Autowired
    private CollectionReadFacadeService collectionReadFacadeService;
    @Autowired
    private ChainFacadeService chainFacadeService;
    @Autowired
    private UserFacadeService userFacadeService;
    @Autowired
    private HeldCollectionService heldCollectionService;

    /**
     * 藏品列表
     * @param state
     * @param keyword
     * @param pageSize
     * @param currentPage
     * @return
     */
    @GetMapping("/collectionList")
    public MultiResult<CollectionVO> collectionList(@NotBlank String state, String keyword, int pageSize, int currentPage){
        CollectionPageQueryRequest collectionPageQueryRequest = new CollectionPageQueryRequest();
        collectionPageQueryRequest.setState(state);
        collectionPageQueryRequest.setKeyword(keyword);
        collectionPageQueryRequest.setCurrentPage(currentPage);
        collectionPageQueryRequest.setPageSize(pageSize);
        PageResponse<CollectionVO> pageResponse = collectionReadFacadeService.pageQuery(collectionPageQueryRequest);
        return MultiResultConvertor.convert(pageResponse);
    }

    /**
     * 用户持有藏品详情
     *
     * @param
     * @return 结果
     */
    @GetMapping("/heldCollectionInfo")
    public Result<HeldCollectionVO> heldCollectionVoResult(@NotBlank String heldCollectionId) {
        SingleResponse<HeldCollectionVO> singleResponse = collectionReadFacadeService.queryHeldCollectionById(Long.valueOf(heldCollectionId));
        return Result.success(singleResponse.getData());
    }

    /**
     * 用户持有藏品列表
     * @param keyword
     * @param state
     * @param pageSize
     * @param currentPage
     * @return
     */
    @GetMapping("/heldCollectionList")
    public MultiResult<HeldCollectionVO> heldCollectionList(String keyword, String state, int pageSize, int currentPage){
        //获取当前登录用户Id
        String userId = (String) StpUtil.getLoginId();
        //创建一个藏品分页查询的请求对象
        HeldCollectionPageQueryRequest heldCollectionPageQueryRequest = new HeldCollectionPageQueryRequest();
        //将传来的持有状态设置为参数
        heldCollectionPageQueryRequest.setState(state);
        //将当前登录用户id设置为参数
        heldCollectionPageQueryRequest.setUserId(userId);
        //将传来的页码设置为参数
        heldCollectionPageQueryRequest.setCurrentPage(currentPage);
        //将传来的当前页显示的总条数设置为参数
        heldCollectionPageQueryRequest.setPageSize(pageSize);
        //将传来的搜索关键字设置为参数
        heldCollectionPageQueryRequest.setKeyword(keyword);
        //利用dubbo 远程调用查询用户持有藏品列表
        PageResponse<HeldCollectionVO> pageResponse = collectionReadFacadeService.pageQueryHeldCollection(heldCollectionPageQueryRequest);
        //传入MultiResultConvertor转换成MultiResult,统一了响应结果对象
        return MultiResultConvertor.convert(pageResponse);
    }

    /**
     * 藏品详情
     * @param collectionId
     * @return
     */
    @GetMapping("/collectionInfo")
    public Result<CollectionVO> collectionInfo(@NotBlank String collectionId){
        //根据传来的藏品id调用Dubbo远程服务查询藏品详情
        SingleResponse<CollectionVO> singleResponse = collectionReadFacadeService.queryById(Long.valueOf(collectionId));
        //返回查询结果
        return Result.success(singleResponse.getData());
    }
}
